'use client'; import { useState, type FormEvent } from 'react'; // Admin-side cancel panel. Sits at the bottom of the admin order page // as a clearly-marked "danger zone". Two-step: open the panel, then // confirm with an optional reason note. The server-side route // (/api/admin/orders/[id]/cancel) handles Stripe refund + customer // email + status flip. Prodigi cancellation is admin's responsibility // out of band if a print order exists. export function AdminCancel({ orderId, status, hasProdigi, }: { orderId: string; status: string; hasProdigi: boolean; }) { const [open, setOpen] = useState(false); const [submitting, setSubmitting] = useState(false); const [reason, setReason] = useState(''); const [error, setError] = useState(null); const [result, setResult] = useState<{ refundId: string | null; refundError: string | null; prodigiWarning: string | null; } | null>(null); // Mirror the server-side rules so the UI doesn't even hint at cancel // for terminal orders. (Terminal = cancelled or abandoned — nothing // to refund, nothing to cancel.) if (status === 'cancelled' || status === 'abandoned') return null; const onCancel = async (e: FormEvent) => { e.preventDefault(); setSubmitting(true); setError(null); // Bound the request so a stalled refund/email never spins the button // forever — surface a clear message and let the admin re-check instead. const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), 45_000); try { const res = await fetch(`/api/admin/orders/${orderId}/cancel`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ reason: reason.trim() || undefined }), signal: controller.signal, }); const data = await res.json().catch(() => null) as { ok?: boolean; refundId?: string | null; refundError?: string | null; prodigiWarning?: string | null; error?: string; } | null; if (!res.ok || !data?.ok) { throw new Error(data?.error ?? `Cancel failed (${res.status})`); } setResult({ refundId: data.refundId ?? null, refundError: data.refundError ?? null, prodigiWarning: data.prodigiWarning ?? null, }); setSubmitting(false); // No router.refresh() here on purpose. The result panel above is a // self-contained success state, and the order list / detail pages are // force-dynamic so they re-fetch fresh on the next navigation anyway. // Calling refresh() invalidated the whole router cache and kicked off a // slow RSC refetch (force-dynamic + tunnel latency), which left the App // Router in a pending transition that froze the next cancel's click. } catch (err) { setError( err instanceof DOMException && err.name === 'AbortError' ? 'This is taking longer than expected — the cancel may still be processing. Refresh the page to check the order status before retrying.' : err instanceof Error ? err.message : 'Cancel failed.', ); setSubmitting(false); } finally { clearTimeout(timer); } }; // Post-cancel result panel — shows refund + Prodigi warnings. if (result) { return (

Order cancelled

); } if (!open) { return (
Danger zone — cancel this order
); } return (

Cancel this order?

This will: refund the full charge via Stripe (if a payment exists), mark the order cancelled, and email the customer. This is reversible only manually — there's no undo button.

{hasProdigi && (

⚠ A Prodigi print order already exists for this commission. Cancelling here does NOT cancel the Prodigi order — you must do that separately via the Prodigi dashboard, or the print will still ship.

)}